package com.addplus.townmall.server.api.mapper.exam;

import com.addplus.townmall.server.api.model.exam.Question;
import com.addplus.townmall.server.api.model.exam.QuestionExt;
import com.addplus.townmall.server.api.utils.BaseAddMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface QuestionMapper extends BaseAddMapper<Question> {
@Select("<script>\n" +
        "SELECT id,answer,content,description,picture,choice_a,choice_b,choice_c,choice_d,choice_e,choice_f,choice_g,choice_h,choice_num,question_type,knowledge_name,difficulty,score,contributor,is_deleted,gmt_create,gmt_modified \n" +
        "FROM question WHERE is_deleted = 0 \n" +
        "<if test=\"questionType!=null and questionType!='所有题型'\">  \n" +
        "    AND question_type = #{questionType}  \n" +
        "</if> \n"+
        "<if test=\"content!=null and content!=''\">  \n"+
        "    AND content like concat('%','${content}','%' )  \n" +
        "</if> "+
        " order by gmt_modified desc\n"+
        "</script>")
List<QuestionExt> getAllQuestionByPage(IPage page,@Param("content")String content,@Param("questionType")String questionType);

    @Select("SELECT id,answer,content,description,picture,choice_a,choice_b,choice_c,choice_d,choice_e,choice_f,choice_g,choice_h,choice_num,question_type,knowledge_name,difficulty,score,contributor,is_deleted,gmt_create,gmt_modified FROM question " +
            "WHERE is_deleted = 0 and question_type=#{questionType} order by gmt_modified desc")
    List<QuestionExt> getAllQuestionByType(IPage page,@Param("questionType")String questinType);


    @Select("<script>\n" +
            "select * from question where id in \n" +
            " <foreach collection='idList' item='item' open='(' separator=',' close=')' >\n" +
            "  #{item}\n" +
            " </foreach>\n"+
            "</script>")
    List<Question> selectQuestionsByIds(@Param("idList")List<Integer> idList);

    @Select("select * from question q where q.question_type=#{questionType} order by rand() limit #{danNum};")
    List<Question> selectDanRand(@Param("questionType")String questionType,@Param("danNum")Integer danNum);

    @Select("<script>\n" +
            "select * from question q \n" +
            "where q.knowledge_name=#{knowledgeName} \n" +
            "<if test=\"difficulty!=null \">  \n" +
            "    AND difficulty = #{difficulty}  \n" +
            "</if> \n"+
            "AND q.question_type=#{questionType} order by rand() limit #{num};\n"+
            "</script>")
    List<Question> selectRandQuestionByTypeAndKnowledge(@Param("knowledgeName")String knowledgeName,@Param("questionType")String questionType,@Param("num")Integer num,@Param("difficulty")String difficulty);

    @Select("select * from question q where q.knowledge_name=#{knowledgeName} order by rand() limit #{num};")
    List<Question> selectRandQuestionByKnowledge(@Param("knowledgeName")String knowledgeName,@Param("num")Integer num);


}
